package design.simdi.code.final_paper.site.panels.licente.candidati;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.spring.injection.annot.SpringBean;

import design.simdi.code.final_paper.core.entities.Domain;
import design.simdi.code.final_paper.core.entities.Project;
import design.simdi.code.final_paper.core.entities.StudentCandidat;
import design.simdi.code.final_paper.core.services.ApplicationService;
import design.simdi.code.final_paper.core.services.ServiceException;
import design.simdi.code.final_paper.core.services.UserService;
import design.simdi.code.final_paper.site.application.ApplicationRoles;
import design.simdi.code.final_paper.site.application.MyAuthenticatedWebSession;
import design.simdi.code.final_paper.utils.ActionToolbar;
import design.simdi.code.final_paper.utils.MyDataTable;
import design.simdi.code.final_paper.utils.StatusStudentCandidat;
import design.simdi.code.final_paper.utils.ToolbarItem;
import design.simdi.code.final_paper.utils.YesNoModalWindow;

/**
 * 
 * @author Marius Dumitriu - (Jun 25, 2010 - 1:28:14 AM)
 * 
 */
public class CandidatiActionToolbar extends ActionToolbar {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@SpringBean
	private ApplicationService applicationService;
	@SpringBean
	private UserService userService;

	private YesNoModalWindow admitYesNoConfirmationModal;
	private YesNoModalWindow rejectYesNoConfirmationModal;

	public CandidatiActionToolbar(MyDataTable<?> table) {
		super(table);

		admitYesNoConfirmationModal = new YesNoModalWindow("admit.confirmation", "confirmare3") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onYes(AjaxRequestTarget target) {
				StudentCandidat sc = (StudentCandidat) getTable().getSelectedRow().getModelObject();
				getApplicationService().changeStatus(sc, StatusStudentCandidat.ACCEPTAT);
			}

			@Override
			public void onNo(AjaxRequestTarget target) {

			}
		};

		admitYesNoConfirmationModal.setOutputMarkupPlaceholderTag(true);

		add(admitYesNoConfirmationModal);

		rejectYesNoConfirmationModal = new YesNoModalWindow("reject.confirmation", "confirmare4") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onYes(AjaxRequestTarget target) {
				StudentCandidat sc = (StudentCandidat) getTable().getSelectedRow().getModelObject();
				getApplicationService().changeStatus(sc, StatusStudentCandidat.RESPINS);
			}

			@Override
			public void onNo(AjaxRequestTarget target) {

			}
		};

		rejectYesNoConfirmationModal.setOutputMarkupPlaceholderTag(true);

		add(rejectYesNoConfirmationModal);
		setOutputMarkupId(true);
	}

	@Override
	public List<ToolbarItem> getItemsList() {
		List<ToolbarItem> lista = new ArrayList<ToolbarItem>();
		if (MyAuthenticatedWebSession.get().isSignedIn()
				&& MyAuthenticatedWebSession.get().getRoles().contains(ApplicationRoles.PROFESSOR)) {
			lista.add(ToolbarItem.ADMIT);
			lista.add(ToolbarItem.REJECT);
		}
		return lista;
	}

	@Override
	public void onAction(ToolbarItem toolbarItem, AjaxRequestTarget target) {

		switch (toolbarItem) {
		case ADMIT:
			onAdmitAction(target);
			break;

		case REJECT:
			onRejectAction(target);
			break;

		}

	}

	private void onRejectAction(AjaxRequestTarget target) {
		try {
			if (getTable().getSelectedRow() == null)
				throw new ServiceException("table.row.not.selected");

			rejectYesNoConfirmationModal.show(target);

		} catch (ServiceException e) {
			warn(getString(e.getMessage()));

		}

	}

	private void onAdmitAction(AjaxRequestTarget target) {
		try {
			if (getTable().getSelectedRow() == null)
				throw new ServiceException("table.row.not.selected");

			StudentCandidat sc = (StudentCandidat) getTable().getSelectedRow().getModelObject();

			Domain d = sc.getDomain();
			Project p = sc.getProject();
			if (d != null)
				if (d.getAvailableProjectsNo() <= 0)
					throw new ServiceException("domain.not.available");

			if (p != null)
				if (!p.isAvailable())
					throw new ServiceException("project.not.available");

			if (!getApplicationService().canBeAdmited(sc))
				throw new ServiceException("student.already.admited");

			admitYesNoConfirmationModal.show(target);

		} catch (ServiceException e) {
			warn(getString(e.getMessage()));

		}

	}

	@Override
	public String getToolbarName(ToolbarItem toolbarItem) {
		return getString(toolbarItem.getName());
	}

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public ApplicationService getApplicationService() {
		return applicationService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserService getUserService() {
		return userService;
	}

}
